//
//  ScocietyViewController.m
//  Moment
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 谭泽源. All rights reserved.
//

#import "ScocietyViewController.h"
#import "Society.h"
#import "PZRMessageHotTableViewCell.h"
#import "PZRSociety.h"
#import "PZRPeopleHotTableViewCell.h"
#import "PZRMusicTableViewCell.h"
#import "PZRMusic1TableViewCell.h"
#import "PZRSocietyHotDetailViewController.h"

@interface ScocietyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIScrollView *mainScrollView;
@property (nonatomic,strong) UITableView  *messageHotTableView;
@property (nonatomic,strong) UITableView  *peopleHotTableView;
@property (nonatomic,strong) NSMutableArray *allArray;
@property (nonatomic,strong) NSMutableArray *allArr;
@end

@implementation ScocietyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"社区";
    self.headNameLabel.text = @"话题";
    self.mainScrollView = [[UIScrollView alloc]init];
    self.mainScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 2, [UIScreen mainScreen].bounds.size.height-64);
    //self.mainScrollView.backgroundColor = [UIColor yellowColor];
    self.mainScrollView.pagingEnabled = YES;
    self.mainScrollView.bounces = NO;
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [self.view addSubview:_mainScrollView];
    [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-64));
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(64);
    }];

    
    self.messageHotTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
    //self.messageHotTableView = [[UITableView alloc]init];
    //self.messageHotTableView.backgroundColor = [UIColor greenColor];
    [self.messageHotTableView registerClass:[PZRMessageHotTableViewCell class]forCellReuseIdentifier:@"meshot"];
    [self.messageHotTableView registerClass:[PZRMusicTableViewCell class] forCellReuseIdentifier:@"meshot1"];
    self.messageHotTableView.delegate = self;
    self.messageHotTableView.dataSource = self;
    [self.mainScrollView addSubview:_messageHotTableView];
//    [self.messageHotTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH));
//        make.top.equalTo(self.mainScrollView.mas_top).offset(0);
//        make.left.equalTo(self.mainScrollView.mas_left).offset(0);
//        make.bottom.equalTo(self.mainScrollView.mas_bottom).offset(0);
//        
//    }];

    
    
    self.allArray = [NSMutableArray array];
    NSString *body = @"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&sort=hot&start=%ld&version=3.0.6";
      [GetData postUrl:@"http://api2.pianke.me/group/posts_hotlist" body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray   *bigArr = [bigDic objectForKey:@"list"];
          for (NSMutableDictionary *dic in bigArr) {
              Society *society = [[Society alloc]init];
              [society setValuesForKeysWithDictionary:dic];
              [self.allArray addObject:society];
              //NSLog(@"%@",society);
              //NSLog(@"%@",self.allArray);
              //NSLog(@"%ld",self.allArray.count);
          }
          [self.messageHotTableView reloadData];
      } failure:^(NSError *error) {
          NSLog(@"失败");
      }];
    
    
    self.peopleHotTableView = [[UITableView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
    //self.peopleHotTableView.backgroundColor = [UIColor blueColor];
    [self.peopleHotTableView registerClass:[PZRPeopleHotTableViewCell class] forCellReuseIdentifier:@"peophot"];
    [self.peopleHotTableView registerClass:[PZRMusic1TableViewCell class] forCellReuseIdentifier:@"peophot1"];
    self.peopleHotTableView.delegate =self;
    self.peopleHotTableView.dataSource =self;
    [self.mainScrollView addSubview:_peopleHotTableView];
    
    self.allArr = [NSMutableArray array];
    NSString *para = @"auth=CJFm1FH259KJDCsF1Gs3ikDJxQMejNrOMom1lsDzRlcWP22NJPSh5a7m34&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&sort=addtime&start=0&version=3.0.6";
    [GetData postUrl:@"http://api2.pianke.me/group/posts_hotlist" body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray   *bigArr = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in bigArr) {
            PZRSociety *society = [[PZRSociety alloc]init];
            [society setValuesForKeysWithDictionary:dic];
            [self.allArr addObject:society];
            //NSLog(@"!!!!%@",self.allArr);
        }
        [self.peopleHotTableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"失败");
    }];

    
    
   
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.messageHotTableView)
    {
        return self.allArray.count;
    }
    if (tableView == self.peopleHotTableView)
    {
        //NSLog(@"###%ld",self.allArray.count);
        return self.allArr.count;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *chongyong1 = @"meshot";
    static NSString *chongyong3 = @"meshot1";
    if (tableView == self.messageHotTableView) {
        Society *society = [[Society alloc]init];
        society = [self.allArray objectAtIndex:indexPath.row];
        if ([society.coverimg isEqualToString:@""])
        {
            PZRMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:chongyong3];
            cell.aSociety = society;
            return cell;
        }
        else
        {
            PZRMessageHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:chongyong1];
            cell.aSociety = society;
            return cell;
        }
    }
    if (tableView == self.peopleHotTableView) {
        static NSString *chongyong2 = @"peophot";
        static NSString *chongyong4 = @"peophot1";
        PZRSociety *society = [[PZRSociety alloc]init];
        society = [self.allArr objectAtIndex:indexPath.row];
        if ([society.coverimg isEqualToString:@""])
        {
            PZRMusic1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:chongyong4];
            cell.aSociety = society;
            return cell;
        }
        else
        {
            PZRPeopleHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:chongyong2];
            cell.aSociety = society;
            return cell;
        }
        }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // PZRSocietyHotDetailViewController *detailVC = [[PZRSocietyHotDetailViewController alloc]init];
   // [self.navigationController pushViewController:detailVC animated:YES];
    
    
    
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
